package com.zzyl.nursing.service.Impl;

import java.util.List;

import com.zzyl.nursing.domain.DTO.NursingPlanDTO;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.domain.VO.NursingPlanVO;
import com.zzyl.nursing.domain.VO.NursingProjectPlanVO;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.service.NursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author zhangzhangzhang
 * @date 2025-09-12
 */
@Service
public class INursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVO selectNursingPlanById(Long id) {
        //根据id查询
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        //由于数据库中字段跟NursingPlanVO字段一致,但类型不一样装不上 所以需要转换
        NursingPlanVO vo = new NursingPlanVO();
        if(nursingPlan==null){
            throw new RuntimeException("没有查询到数据");
        }
        BeanUtils.copyProperties(nursingPlan,vo);

        //第二步 查询护理计划项目计划即中间表
        List<NursingProjectPlanVO> nursingProjectPlans = nursingProjectPlanService.listByPlanId(id);
        if(nursingProjectPlans!=null){
            vo.setProjectPlans(nursingProjectPlans);
        }
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO nursingPlanDTO) {
        //新增护理计划  怎么新增 ??? 新增的是什么--新增护理计划项目 以及 护理项目计划中间表
        //第一步需要的是与数据库相对应的实体类 而参数不满足这个需求 所以需要new对象收集
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO, nursingPlan);
        this.save(nursingPlan);
        //第二步将中间表数据保存在中间表中 ------这个第二种方法就是跟之前一样需要在mapper层写方法 在xml里写插入语句 遍历插入数据库中
        //上面那个麻烦  下面这个为啥不用mapper里群插方法是因为(用的spring boot是2.7版本)mapper没有这个方法 但是service层有这个方法 所以注入service层
        if(nursingPlanDTO.getProjectPlans()!=null && nursingPlanDTO.getProjectPlans().size()>0){
            //设置id
            nursingPlanDTO.getProjectPlans().forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.saveBatch(nursingPlanDTO.getProjectPlans());
        }
       return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan) {
        return updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
